1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.TlsCertificate;
26 
27 private import gio.SocketConnectableIF;
28 private import gio.c.functions;
29 public  import gio.c.types;
30 private import glib.ConstructionException;
31 private import glib.DateTime;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.ListG;
35 private import glib.PtrArray;
36 private import glib.Str;
37 private import glib.c.functions;
38 private import gobject.ObjectG;
39 
40 
41 /**
42  * A certificate used for TLS authentication and encryption.
43  * This can represent either a certificate only (eg, the certificate
44  * received by a client from a server), or the combination of
45  * a certificate and a private key (which is needed when acting as a
46  * #GTlsServerConnection).
47  *
48  * Since: 2.28
49  */
50 public class TlsCertificate : ObjectG
51 {
52 	/** the main Gtk struct */
53 	protected GTlsCertificate* gTlsCertificate;
54 
55 	/** Get the main Gtk struct */
56 	public GTlsCertificate* getTlsCertificateStruct(bool transferOwnership = false)
57 	{
58 		if (transferOwnership)
59 			ownedRef = false;
60 		return gTlsCertificate;
61 	}
62 
63 	/** the main Gtk struct as a void* */
64 	protected override void* getStruct()
65 	{
66 		return cast(void*)gTlsCertificate;
67 	}
68 
69 	/**
70 	 * Sets our main struct and passes it to the parent class.
71 	 */
72 	public this (GTlsCertificate* gTlsCertificate, bool ownedRef = false)
73 	{
74 		this.gTlsCertificate = gTlsCertificate;
75 		super(cast(GObject*)gTlsCertificate, ownedRef);
76 	}
77 
78 	/**
79 	 * Creates a #GTlsCertificate from the PEM-encoded data in @cert_file
80 	 * and @key_file. The returned certificate will be the first certificate
81 	 * found in @cert_file. As of GLib 2.44, if @cert_file contains more
82 	 * certificates it will try to load a certificate chain. All
83 	 * certificates will be verified in the order found (top-level
84 	 * certificate should be the last one in the file) and the
85 	 * #GTlsCertificate:issuer property of each certificate will be set
86 	 * accordingly if the verification succeeds. If any certificate in the
87 	 * chain cannot be verified, the first certificate in the file will
88 	 * still be returned.
89 	 *
90 	 * If either file cannot be read or parsed, the function will return
91 	 * %NULL and set @error. Otherwise, this behaves like
92 	 * g_tls_certificate_new_from_pem().
93 	 *
94 	 * Params:
95 	 *     certFile = file containing one or more PEM-encoded
96 	 *         certificates to import
97 	 *     keyFile = file containing a PEM-encoded private key
98 	 *         to import
99 	 *
100 	 * Returns: the new certificate, or %NULL on error
101 	 *
102 	 * Since: 2.28
103 	 *
104 	 * Throws: GException on failure.
105 	 * Throws: ConstructionException GTK+ fails to create the object.
106 	 */
107 	public static TlsCertificate newFromFiles(string certFile, string keyFile)
108 	{
109 		GError* err = null;
110 
111 		auto __p = g_tls_certificate_new_from_files(Str.toStringz(certFile), Str.toStringz(keyFile), &err);
112 
113 		if (err !is null)
114 		{
115 			throw new GException( new ErrorG(err) );
116 		}
117 
118 		if(__p is null)
119 		{
120 			throw new ConstructionException("null returned by new_from_files");
121 		}
122 
123 		return new TlsCertificate(cast(GTlsCertificate*) __p, true);
124 	}
125 
126 	/**
127 	 * Creates a #GTlsCertificate from a PKCS \#11 URI.
128 	 *
129 	 * An example @pkcs11_uri would be `pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01`
130 	 *
131 	 * Where the token’s layout is:
132 	 *
133 	 * ```
134 	 * Object 0:
135 	 * URL: pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01;object=private%20key;type=private
136 	 * Type: Private key (RSA-2048)
137 	 * ID: 01
138 	 *
139 	 * Object 1:
140 	 * URL: pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01;object=Certificate%20for%20Authentication;type=cert
141 	 * Type: X.509 Certificate (RSA-2048)
142 	 * ID: 01
143 	 * ```
144 	 *
145 	 * In this case the certificate and private key would both be detected and used as expected.
146 	 * @pkcs_uri may also just reference an X.509 certificate object and then optionally
147 	 * @private_key_pkcs11_uri allows using a private key exposed under a different URI.
148 	 *
149 	 * Note that the private key is not accessed until usage and may fail or require a PIN later.
150 	 *
151 	 * Params:
152 	 *     pkcs11Uri = A PKCS \#11 URI
153 	 *     privateKeyPkcs11Uri = A PKCS \#11 URI
154 	 *
155 	 * Returns: the new certificate, or %NULL on error
156 	 *
157 	 * Since: 2.68
158 	 *
159 	 * Throws: GException on failure.
160 	 * Throws: ConstructionException GTK+ fails to create the object.
161 	 */
162 	public static TlsCertificate newFromPkcs11Uris(string pkcs11Uri, string privateKeyPkcs11Uri)
163 	{
164 		GError* err = null;
165 
166 		auto __p = g_tls_certificate_new_from_pkcs11_uris(Str.toStringz(pkcs11Uri), Str.toStringz(privateKeyPkcs11Uri), &err);
167 
168 		if (err !is null)
169 		{
170 			throw new GException( new ErrorG(err) );
171 		}
172 
173 		if(__p is null)
174 		{
175 			throw new ConstructionException("null returned by new_from_pkcs11_uris");
176 		}
177 
178 		return new TlsCertificate(cast(GTlsCertificate*) __p, true);
179 	}
180 
181 	/**
182 	 */
183 
184 	/** */
185 	public static GType getType()
186 	{
187 		return g_tls_certificate_get_type();
188 	}
189 
190 	/**
191 	 * Creates a #GTlsCertificate from the data in @file.
192 	 *
193 	 * As of 2.72, if the filename ends in `.p12` or `.pfx` the data is loaded by
194 	 * g_tls_certificate_new_from_pkcs12() otherwise it is loaded by
195 	 * g_tls_certificate_new_from_pem(). See those functions for
196 	 * exact details.
197 	 *
198 	 * If @file cannot be read or parsed, the function will return %NULL and
199 	 * set @error.
200 	 *
201 	 * Params:
202 	 *     file = file containing a certificate to import
203 	 *
204 	 * Returns: the new certificate, or %NULL on error
205 	 *
206 	 * Since: 2.28
207 	 *
208 	 * Throws: GException on failure.
209 	 * Throws: ConstructionException GTK+ fails to create the object.
210 	 */
211 	public this(string file)
212 	{
213 		GError* err = null;
214 
215 		auto __p = g_tls_certificate_new_from_file(Str.toStringz(file), &err);
216 
217 		if (err !is null)
218 		{
219 			throw new GException( new ErrorG(err) );
220 		}
221 
222 		if(__p is null)
223 		{
224 			throw new ConstructionException("null returned by new_from_file");
225 		}
226 
227 		this(cast(GTlsCertificate*) __p, true);
228 	}
229 
230 	/**
231 	 * Creates a #GTlsCertificate from the data in @file.
232 	 *
233 	 * If @file cannot be read or parsed, the function will return %NULL and
234 	 * set @error.
235 	 *
236 	 * Any unknown file types will error with %G_IO_ERROR_NOT_SUPPORTED.
237 	 * Currently only `.p12` and `.pfx` files are supported.
238 	 * See g_tls_certificate_new_from_pkcs12() for more details.
239 	 *
240 	 * Params:
241 	 *     file = file containing a certificate to import
242 	 *     password = password for PKCS #12 files
243 	 *
244 	 * Returns: the new certificate, or %NULL on error
245 	 *
246 	 * Since: 2.72
247 	 *
248 	 * Throws: GException on failure.
249 	 * Throws: ConstructionException GTK+ fails to create the object.
250 	 */
251 	public this(string file, string password)
252 	{
253 		GError* err = null;
254 
255 		auto __p = g_tls_certificate_new_from_file_with_password(Str.toStringz(file), Str.toStringz(password), &err);
256 
257 		if (err !is null)
258 		{
259 			throw new GException( new ErrorG(err) );
260 		}
261 
262 		if(__p is null)
263 		{
264 			throw new ConstructionException("null returned by new_from_file_with_password");
265 		}
266 
267 		this(cast(GTlsCertificate*) __p, true);
268 	}
269 
270 	/**
271 	 * Creates a #GTlsCertificate from the PEM-encoded data in @data. If
272 	 * @data includes both a certificate and a private key, then the
273 	 * returned certificate will include the private key data as well. (See
274 	 * the #GTlsCertificate:private-key-pem property for information about
275 	 * supported formats.)
276 	 *
277 	 * The returned certificate will be the first certificate found in
278 	 * @data. As of GLib 2.44, if @data contains more certificates it will
279 	 * try to load a certificate chain. All certificates will be verified in
280 	 * the order found (top-level certificate should be the last one in the
281 	 * file) and the #GTlsCertificate:issuer property of each certificate
282 	 * will be set accordingly if the verification succeeds. If any
283 	 * certificate in the chain cannot be verified, the first certificate in
284 	 * the file will still be returned.
285 	 *
286 	 * Params:
287 	 *     data = PEM-encoded certificate data
288 	 *     length = the length of @data, or -1 if it's 0-terminated.
289 	 *
290 	 * Returns: the new certificate, or %NULL if @data is invalid
291 	 *
292 	 * Since: 2.28
293 	 *
294 	 * Throws: GException on failure.
295 	 * Throws: ConstructionException GTK+ fails to create the object.
296 	 */
297 	public this(string data, ptrdiff_t length)
298 	{
299 		GError* err = null;
300 
301 		auto __p = g_tls_certificate_new_from_pem(Str.toStringz(data), length, &err);
302 
303 		if (err !is null)
304 		{
305 			throw new GException( new ErrorG(err) );
306 		}
307 
308 		if(__p is null)
309 		{
310 			throw new ConstructionException("null returned by new_from_pem");
311 		}
312 
313 		this(cast(GTlsCertificate*) __p, true);
314 	}
315 
316 	/**
317 	 * Creates a #GTlsCertificate from the data in @data. It must contain
318 	 * a certificate and matching private key.
319 	 *
320 	 * If extra certificates are included they will be verified as a chain
321 	 * and the #GTlsCertificate:issuer property will be set.
322 	 * All other data will be ignored.
323 	 *
324 	 * You can pass as single password for all of the data which will be
325 	 * used both for the PKCS #12 container as well as encrypted
326 	 * private keys. If decryption fails it will error with
327 	 * %G_TLS_ERROR_BAD_CERTIFICATE_PASSWORD.
328 	 *
329 	 * This constructor requires support in the current #GTlsBackend.
330 	 * If support is missing it will error with
331 	 * %G_IO_ERROR_NOT_SUPPORTED.
332 	 *
333 	 * Other parsing failures will error with %G_TLS_ERROR_BAD_CERTIFICATE.
334 	 *
335 	 * Params:
336 	 *     data = DER-encoded PKCS #12 format certificate data
337 	 *     password = optional password for encrypted certificate data
338 	 *
339 	 * Returns: the new certificate, or %NULL if @data is invalid
340 	 *
341 	 * Since: 2.72
342 	 *
343 	 * Throws: GException on failure.
344 	 * Throws: ConstructionException GTK+ fails to create the object.
345 	 */
346 	public this(ubyte[] data, string password)
347 	{
348 		GError* err = null;
349 
350 		auto __p = g_tls_certificate_new_from_pkcs12(data.ptr, cast(size_t)data.length, Str.toStringz(password), &err);
351 
352 		if (err !is null)
353 		{
354 			throw new GException( new ErrorG(err) );
355 		}
356 
357 		if(__p is null)
358 		{
359 			throw new ConstructionException("null returned by new_from_pkcs12");
360 		}
361 
362 		this(cast(GTlsCertificate*) __p, true);
363 	}
364 
365 	/**
366 	 * Creates one or more #GTlsCertificates from the PEM-encoded
367 	 * data in @file. If @file cannot be read or parsed, the function will
368 	 * return %NULL and set @error. If @file does not contain any
369 	 * PEM-encoded certificates, this will return an empty list and not
370 	 * set @error.
371 	 *
372 	 * Params:
373 	 *     file = file containing PEM-encoded certificates to import
374 	 *
375 	 * Returns: a
376 	 *     #GList containing #GTlsCertificate objects. You must free the list
377 	 *     and its contents when you are done with it.
378 	 *
379 	 * Since: 2.28
380 	 *
381 	 * Throws: GException on failure.
382 	 */
383 	public static ListG listNewFromFile(string file)
384 	{
385 		GError* err = null;
386 
387 		auto __p = g_tls_certificate_list_new_from_file(Str.toStringz(file), &err);
388 
389 		if (err !is null)
390 		{
391 			throw new GException( new ErrorG(err) );
392 		}
393 
394 		if(__p is null)
395 		{
396 			return null;
397 		}
398 
399 		return new ListG(cast(GList*) __p, true);
400 	}
401 
402 	/**
403 	 * Gets the value of #GTlsCertificate:dns-names.
404 	 *
405 	 * Returns: A #GPtrArray of
406 	 *     #GBytes elements, or %NULL if it's not available.
407 	 *
408 	 * Since: 2.70
409 	 */
410 	public PtrArray getDnsNames()
411 	{
412 		auto __p = g_tls_certificate_get_dns_names(gTlsCertificate);
413 
414 		if(__p is null)
415 		{
416 			return null;
417 		}
418 
419 		return new PtrArray(cast(GPtrArray*) __p);
420 	}
421 
422 	/**
423 	 * Gets the value of #GTlsCertificate:ip-addresses.
424 	 *
425 	 * Returns: A #GPtrArray
426 	 *     of #GInetAddress elements, or %NULL if it's not available.
427 	 *
428 	 * Since: 2.70
429 	 */
430 	public PtrArray getIpAddresses()
431 	{
432 		auto __p = g_tls_certificate_get_ip_addresses(gTlsCertificate);
433 
434 		if(__p is null)
435 		{
436 			return null;
437 		}
438 
439 		return new PtrArray(cast(GPtrArray*) __p);
440 	}
441 
442 	/**
443 	 * Gets the #GTlsCertificate representing @cert's issuer, if known
444 	 *
445 	 * Returns: The certificate of @cert's issuer,
446 	 *     or %NULL if @cert is self-signed or signed with an unknown
447 	 *     certificate.
448 	 *
449 	 * Since: 2.28
450 	 */
451 	public TlsCertificate getIssuer()
452 	{
453 		auto __p = g_tls_certificate_get_issuer(gTlsCertificate);
454 
455 		if(__p is null)
456 		{
457 			return null;
458 		}
459 
460 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p);
461 	}
462 
463 	/**
464 	 * Returns the issuer name from the certificate.
465 	 *
466 	 * Returns: The issuer name, or %NULL if it's not available.
467 	 *
468 	 * Since: 2.70
469 	 */
470 	public string getIssuerName()
471 	{
472 		auto retStr = g_tls_certificate_get_issuer_name(gTlsCertificate);
473 
474 		scope(exit) Str.freeString(retStr);
475 		return Str.toString(retStr);
476 	}
477 
478 	/**
479 	 * Returns the time at which the certificate became or will become invalid.
480 	 *
481 	 * Returns: The not-valid-after date, or %NULL if it's not available.
482 	 *
483 	 * Since: 2.70
484 	 */
485 	public DateTime getNotValidAfter()
486 	{
487 		auto __p = g_tls_certificate_get_not_valid_after(gTlsCertificate);
488 
489 		if(__p is null)
490 		{
491 			return null;
492 		}
493 
494 		return new DateTime(cast(GDateTime*) __p, true);
495 	}
496 
497 	/**
498 	 * Returns the time at which the certificate became or will become valid.
499 	 *
500 	 * Returns: The not-valid-before date, or %NULL if it's not available.
501 	 *
502 	 * Since: 2.70
503 	 */
504 	public DateTime getNotValidBefore()
505 	{
506 		auto __p = g_tls_certificate_get_not_valid_before(gTlsCertificate);
507 
508 		if(__p is null)
509 		{
510 			return null;
511 		}
512 
513 		return new DateTime(cast(GDateTime*) __p, true);
514 	}
515 
516 	/**
517 	 * Returns the subject name from the certificate.
518 	 *
519 	 * Returns: The subject name, or %NULL if it's not available.
520 	 *
521 	 * Since: 2.70
522 	 */
523 	public string getSubjectName()
524 	{
525 		auto retStr = g_tls_certificate_get_subject_name(gTlsCertificate);
526 
527 		scope(exit) Str.freeString(retStr);
528 		return Str.toString(retStr);
529 	}
530 
531 	/**
532 	 * Check if two #GTlsCertificate objects represent the same certificate.
533 	 * The raw DER byte data of the two certificates are checked for equality.
534 	 * This has the effect that two certificates may compare equal even if
535 	 * their #GTlsCertificate:issuer, #GTlsCertificate:private-key, or
536 	 * #GTlsCertificate:private-key-pem properties differ.
537 	 *
538 	 * Params:
539 	 *     certTwo = second certificate to compare
540 	 *
541 	 * Returns: whether the same or not
542 	 *
543 	 * Since: 2.34
544 	 */
545 	public bool isSame(TlsCertificate certTwo)
546 	{
547 		return g_tls_certificate_is_same(gTlsCertificate, (certTwo is null) ? null : certTwo.getTlsCertificateStruct()) != 0;
548 	}
549 
550 	/**
551 	 * This verifies @cert and returns a set of #GTlsCertificateFlags
552 	 * indicating any problems found with it. This can be used to verify a
553 	 * certificate outside the context of making a connection, or to
554 	 * check a certificate against a CA that is not part of the system
555 	 * CA database.
556 	 *
557 	 * If @identity is not %NULL, @cert's name(s) will be compared against
558 	 * it, and %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the return
559 	 * value if it does not match. If @identity is %NULL, that bit will
560 	 * never be set in the return value.
561 	 *
562 	 * If @trusted_ca is not %NULL, then @cert (or one of the certificates
563 	 * in its chain) must be signed by it, or else
564 	 * %G_TLS_CERTIFICATE_UNKNOWN_CA will be set in the return value. If
565 	 * @trusted_ca is %NULL, that bit will never be set in the return
566 	 * value.
567 	 *
568 	 * GLib guarantees that if certificate verification fails, at least one
569 	 * error will be set in the return value, but it does not guarantee
570 	 * that all possible errors will be set. Accordingly, you may not safely
571 	 * decide to ignore any particular type of error. For example, it would
572 	 * be incorrect to mask %G_TLS_CERTIFICATE_EXPIRED if you want to allow
573 	 * expired certificates, because this could potentially be the only
574 	 * error flag set even if other problems exist with the certificate.
575 	 *
576 	 * Because TLS session context is not used, #GTlsCertificate may not
577 	 * perform as many checks on the certificates as #GTlsConnection would.
578 	 * For example, certificate constraints may not be honored, and
579 	 * revocation checks may not be performed. The best way to verify TLS
580 	 * certificates used by a TLS connection is to let #GTlsConnection
581 	 * handle the verification.
582 	 *
583 	 * Params:
584 	 *     identity = the expected peer identity
585 	 *     trustedCa = the certificate of a trusted authority
586 	 *
587 	 * Returns: the appropriate #GTlsCertificateFlags
588 	 *
589 	 * Since: 2.28
590 	 */
591 	public GTlsCertificateFlags verify(SocketConnectableIF identity, TlsCertificate trustedCa)
592 	{
593 		return g_tls_certificate_verify(gTlsCertificate, (identity is null) ? null : identity.getSocketConnectableStruct(), (trustedCa is null) ? null : trustedCa.getTlsCertificateStruct());
594 	}
595 }